<?php
    /******************************************************************************
     *    Copyright ZhangYue
     * Author:                Quinn Xu
     * Description:           
     * Want argument:         SELECT banji FROM chengji GROUP BY banji
     * Output:                
     * Create Date:           2012-12-18
     *****************************************************************************/
    include_once 'config.inc.php';
    include_once DATACLASS . 'chengji.class.php';
    include_once DATACLASS . 'chengjiguanli.class.php';
    include_once DATACLASS . 'peizhi.class.php';
    include_once DATACLASS . 'peizhiguanli.class.php';
    $chengji = new chengji();
    $chengjiguanli = new chengjiguanli();
    $peizhi = new peizhi();
    $peizhiguanli = new peizhiguanli();
    
    //获取班级列表
    $peizhi->setShuxingming('banjiliebiao');
    $banjiliebiao = $peizhiguanli->tongguoshuxingminghuoqushuxingzhi($peizhi);
    $banjiliebiaoshuzu = explode(',', $banjiliebiao[0]['shuxingzhi']);
    
    
    $zuihoumingcishuzu = array('A层次最后名次','B层次最后名次','C层次最后名次','D层次最后名次');
    $mingciqizhi = array('A层次','B层次','C层次','D层次');
    $cengcigeshu = count($mingciqizhi);//用于设置行数
    $cengcishuzu = array();//层次数组,三维数组,一维是层次,二维是系数和班级,三维是人数 总分
    $xishuliebiao = array();//各层次系数
    $banjizongfen = array();//班级总分
    //获取层次系数
    foreach($mingciqizhi as $value)
    {
        $peizhi->setShuxingming($value);
        $xishu = $peizhiguanli->tongguoshuxingminghuoqushuxingzhi($peizhi);
        $xishuliebiao[$value]['系数'] = $xishu[0]['shuxingzhi'];
    }
    foreach($zuihoumingcishuzu as $key=>$value)
    {
        $peizhi->setShuxingming($value);
        $temp = $peizhiguanli->tongguoshuxingminghuoqushuxingzhi($peizhi);
        $jiezhimingci = $temp[0]['shuxingzhi'];
        //当前层次最后名次
        if(!$jiezhimingci)
        {
            $jiezhimingci = 0;//当前层次最后名次
            $qishimingci = 0;//当前层次起始名次
            $shangcimingci = 0;//上次最后名次
        }
        else
        {
            $qishimingci = $shangcimingci;
            $shangcimingci = $jiezhimingci;//临时变量,用做下一层次的起始名次
        }
        
        
        $cengcishuzu[$mingciqizhi[$key]]['系数'] = $xishuliebiao[$mingciqizhi[$key]]['系数'];//系数放在第二维
        //班级数组
        foreach($banjiliebiaoshuzu as $banji)
        {
            $chengji->setQishimingci($qishimingci);
            $chengji->setJiezhimingci($jiezhimingci);
            $chengji->setBanji($banji);
            $result = $chengjiguanli->huoqucengcirenshu($chengji);
            $cengcishuzu[$mingciqizhi[$key]][$banji]['人数'] = $result[0]['zongrenshu'];//人数放在第三维
            $cengcishuzu[$mingciqizhi[$key]][$banji]['得分'] = $cengcishuzu[$mingciqizhi[$key]][$banji]['人数'] * $cengcishuzu[$mingciqizhi[$key]]['系数'];//得分放在第三维
            $banjizongfen[$banji]['总分'] += $cengcishuzu[$mingciqizhi[$key]][$banji]['得分'];//班级总分
        }
    }
    
    
    
    //计算年级总分
    $nianjizongfen = '';
    foreach($banjiliebiaoshuzu as $key=>$value)
    {
        $nianjizongfen += $banjizongfen[$value]['总分'];
    }
    
    
    //获取班级得分总金额
    $banjidefenzongjine = '';
    $peizhi->setShuxingming('班级得分总金额');
    $banjidefenzongjine = $peizhiguanli->tongguoshuxingminghuoqushuxingzhi($peizhi);
    $banjidefenzongjine = $banjidefenzongjine['0']['shuxingzhi'];
    
    //获取单价,精确计算,不四舍五入
    $danjia = $banjidefenzongjine / $nianjizongfen;
    
    //获取金额小数点后位数
    $jinexiaoshudianhouweishu = '';
    $peizhi->setShuxingming('金额小数点后保留位数');
    $jinexiaoshudianhouweishu = $peizhiguanli->tongguoshuxingminghuoqushuxingzhi($peizhi);
    $jinexiaoshudianhouweishu = $jinexiaoshudianhouweishu[0]['shuxingzhi'];
    
    //获取各班金额,四舍五入
    foreach($banjizongfen as $key=>$value)
    {
        $banjizongfen[$key]['总金额'] = round($danjia * $value['zongfen'],$jinexiaoshudianhouweishu);
    }
    
    //四舍五入后的年级总金额
    foreach($banjizongfen as $key=>$value)
    {
        $sishewuruzongjine += $value['总金额'];
    }
    echo '<pre>';
    print_r($cengcishuzu);
    echo '</pre>';
    $smarty->assign('cengcishuzu',$cengcishuzu);
    $smarty->assign('banjiliebiao',$banjiliebiaoshuzu);
    $smarty->assign('banjizongfen',$banjizongfen);
    $smarty->assign('nianjizongfen',$nianjizongfen);
    $smarty->assign('danjia',$danjia);
    $smarty->assign('sishewuruzongjine',$sishewuruzongjine);
    $smarty->display('banjidefen.html');
    
?>